namespace GYLite
{
    export class URLVariables{
		public variables:any;
		public constructor(source) {
			if (source === void 0) { source = null; }
			let s = this;
			s.variables = null;
			if (source !== null) {
				s.decode(source);
			}
		}
		public decode(source) {
			if (!this.variables) {
				this.variables = {};
			}
			source = source.split("+").join(" ");
			var tokens, re = /[?&]?([^=]+)=([^&]*)/g;
			while (tokens = re.exec(source)) {
				var key = decodeURIComponent(tokens[1]), val = decodeURIComponent(tokens[2]);
				//没有重复键值，直接赋值
				if ((key in this.variables) == false) {
					this.variables[key] = val;
					continue;
				}
				//有重复键值，如果已经存在数组，直接push到数组，否则创建一个新数组
				var value = this.variables[key];
				if (value instanceof Array) {
					value.push(val);
				}
				else {
					this.variables[key] = [value, val];
				}
			}
		}
		public toString() {
			if (!this.variables) {
				return "";
			}
			var variables = this.variables;
			var stringArray = [];
			for (var key in variables) {
				stringArray.push(this.encodeValue(key, variables[key]));
			}
			return stringArray.join("&");
		}
		public encodeValue(key, value) {
			if (value instanceof Array) {
				return this.encodeArray(key, value);
			}
			else {
				return encodeURIComponent(key) + "=" + encodeURIComponent(value);
			}
		}
		public encodeArray(key, value) {
			if (!key)
				return "";
			if (value.length == 0) {
				return encodeURIComponent(key) + "=";
			}
			return value.map(function (v) { return encodeURIComponent(key) + "=" + encodeURIComponent(v); }).join("&");
		}
	}
}